package com.basics.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basics.api.enums.socket.MessageEnum;
import com.basics.api.response.channel.ChannelListData;
import com.basics.api.response.channel.ChannelMsgListData;
import com.basics.api.response.channel.ChannelUserListData;
import com.basics.api.response.channel.PostOrMsgData;
import com.basics.channel.entity.CyChannel;
import com.basics.channel.mapper.CyChannelMapper;
import com.basics.channel.service.CyChannelMsgService;
import com.basics.channel.service.CyChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basics.common.utils.DateUtils;
import com.basics.common.utils.StringUtils;
import com.basics.post.entity.CyChannelPost;
import com.basics.post.entity.CyChannelPostUser;
import com.basics.post.service.CyChannelPostService;
import com.basics.post.service.CyChannelPostUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 频道信息 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-03-09
 */
@Service
public class CyChannelServiceImpl extends ServiceImpl<CyChannelMapper, CyChannel> implements CyChannelService {

    @Autowired
    private CyChannelPostService cyChannelPostService;
    @Autowired
    private CyChannelMsgService cyChannelMsgService;


    public CyChannel getChannelInfo(Long id,Long organizationId){
        return getOne(new QueryWrapper<CyChannel>().eq("del_flag",0).eq("id",id).eq("organization_id",organizationId));
    }

    @Override
    public List<ChannelListData> selectHomeChannelList(Long organizationId, Long userId){
        List<ChannelListData> channelList =  baseMapper.selectHomeChannelList(organizationId,userId,null);
        if(CollectionUtils.isNotEmpty(channelList)){
            channelList.forEach(v->{
                setData(v,userId);
            });
        }
        return channelList;
    }

    @Override
    public ChannelListData getChannelInfo(Long organizationId, Long userId,Long channelId){
        List<ChannelListData> vList =  baseMapper.selectHomeChannelList(organizationId,userId,channelId);
        if(CollectionUtils.isNotEmpty(vList)){
            ChannelListData v = vList.get(0);
            setData(v,userId);
            return v;
        }
        return null;
    }

    @Override
    public List<ChannelListData> selectChannelInfoForMsg(Long organizationId, Long channelId, MessageEnum messageEnum){
        List<ChannelUserListData> msgList =  baseMapper.selectChannelInfoForMsg(organizationId,channelId);
        List<ChannelListData> resList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(msgList)){
            int postNum = cyChannelPostService.count(new QueryWrapper<CyChannelPost>().eq("del_flag",0).eq("channel_id",channelId));
            CyChannel cyChannel = getById(channelId);
            msgList.forEach(v->{
                ChannelListData listData = new ChannelListData();
                BeanUtils.copyProperties(cyChannel,listData);
                listData.setMsgNum(v.getMsgNum());
                int unRead = postNum-v.getReadNum();
                if(unRead<0){
                    unRead = 0;
                }
                listData.setUnReadNum(unRead);
                listData.setAllMsgNum(v.getAllMsgNum());
                listData.setUserId(v.getUserId());
                if(!MessageEnum.DELETE_CHANNEL.equals(messageEnum)){
                    setData(listData,v.getUserId());
                }
                resList.add(listData);
            });
        }
        return resList;
    }

    private void setData(ChannelListData v,Long userId){
        PostOrMsgData post = cyChannelPostService.getLastPost(v.getId());
        if(post!=null){
            String content = "";
            if(StringUtils.isNotEmpty(post.getNickName())){
                content = post.getNickName()+":";
            }
            if(StringUtils.isNotEmpty(post.getContent())){
                content = content+post.getContent();
            }
            v.setContent(content);
            v.setNickName(post.getNickName());
            if(v.getShowTime()==null){
                v.setShowTime(DateUtils.getTimeStamp(post.getShowTime()));
            }else{
                if(v.getShowTime()<post.getShowTime().getTime()){
                    v.setShowTime(DateUtils.getTimeStamp(post.getShowTime()));
                }
            }

        }
        PostOrMsgData msg = cyChannelMsgService.getLastMsg(v.getId(),userId);
        if(msg!=null){
            if(v.getShowTime()==null){
                v.setShowTime(DateUtils.getTimeStamp(msg.getShowTime()));
            }else{
                if(v.getShowTime()<msg.getShowTime().getTime()){
                    v.setShowTime(DateUtils.getTimeStamp(msg.getShowTime()));
                }
            }
            if(!msg.getReadFlag()){
                v.setMsgRemindType(msg.getSendType());
            }
            v.setAvatar(msg.getAvatar());
        }
    }
}
